package com.sr.examination.main;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sr.examination.R;
import com.sr.examination.common.event.UserHeadEvent;
import com.sr.examination.exam.grade.ExamGradeActivity;
import com.sr.examination.net.Net;
import com.sr.examination.other.AboutActivity;
import com.sr.examination.other.FeedBackActivity;
import com.sr.examination.other.HelpActivity;
import com.sr.examination.user.Account;
import com.sr.examination.user.base.LoginActivity;
import com.sr.examination.user.base.UserInfoActivity;
import com.sr.examination.user.wallet.WalletActivity;
import com.tl.commonlibrary.event.EventUtil;
import com.tl.commonlibrary.event.LoginStatusEvent;
import com.tl.commonlibrary.network.ErrorResponse;
import com.tl.commonlibrary.network.RequestListener;
import com.tl.commonlibrary.network.bean.base.BaseBean;
import com.tl.commonlibrary.tool.DeviceUtil;
import com.tl.commonlibrary.tool.UIUtils;
import com.tl.commonlibrary.ui.BaseFragment;
import com.tl.commonlibrary.ui.widget.CustomAlertDialog;
import com.tl.commonlibrary.ui.widget.GlideRoundTransform;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import retrofit2.Call;

/**
 * Created by jiangzhongshan on 18/1/30.
 */

public class MenuFragment extends BaseFragment {
    private MenuListener listener;
    private ImageView headIView;
    private TextView nameTView, logoutBtn;
    private CustomAlertDialog logoutDialog;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.frag_main_menu_layout, null);
        EventUtil.register(this);
        root.findViewById(R.id.userInfoLayout).setOnClickListener(this);
        root.findViewById(R.id.walletMenuView).setOnClickListener(this);
        root.findViewById(R.id.guideMenuView).setOnClickListener(this);
        root.findViewById(R.id.examLevelMenuView).setOnClickListener(this);
        root.findViewById(R.id.feedbackMenuView).setOnClickListener(this);
        root.findViewById(R.id.appScoreMenuView).setOnClickListener(this);
        root.findViewById(R.id.aboutMenuView).setOnClickListener(this);
        logoutBtn = root.findViewById(R.id.logoutBtn);
        logoutBtn.setOnClickListener(this);
        headIView = root.findViewById(R.id.headIView);
        nameTView = root.findViewById(R.id.nameTView);
        initData();
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    public void setListener(MenuListener listener) {
        this.listener = listener;
    }

    private void initData() {
        Glide.with(this).load(Account.isLogged() ? Account.getUser().getProfilePhoto() : "").error(R.mipmap.ic_head_default).placeholder(R.mipmap.ic_head_default)
                .transform(new GlideRoundTransform(context, 70))
                .into(headIView);
        if (Account.isLogged()) {
            nameTView.setText(Account.getUser().getNickname());
            logoutBtn.setVisibility(View.VISIBLE);
        } else {
            nameTView.setText(R.string.un_login);
            logoutBtn.setVisibility(View.INVISIBLE);
        }
    }

    private void logout() {
        showProgressDialog(getString(R.string.dialog_logout), false);
        Net.logout(new RequestListener<BaseBean>() {
            @Override
            public void onSuccess(Call<BaseBean> call, BaseBean response) {
                Account.logOff();
                UIUtils.showToast(getString(R.string.hint_logout_success));
                dismissProgressDialog();
            }

            @Override
            public void onFailed(Call<BaseBean> call, ErrorResponse errorResponse) {
                dismissProgressDialog();
            }
        });
    }

    private void showLogoutDialog() {
        if (logoutDialog == null) {
            logoutDialog = new CustomAlertDialog(context);
            logoutDialog.setContent(getString(R.string.hint_logout));
            logoutDialog.setListener(new CustomAlertDialog.OnAlertClickListener() {
                @Override
                public void onRightClicked(View view) {
                    logout();
                }

                @Override
                public void onLeftClicked(View view) {

                }
            });
        }
        logoutDialog.show();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(UserHeadEvent event) {
        if (!TextUtils.isEmpty(event.headUrl)) {
            Glide.with(this).load(Account.isLogged() ? event.headUrl : "").error(R.mipmap.ic_head_default).placeholder(R.mipmap.ic_head_default)
                    .transform(new GlideRoundTransform(context, 70))
                    .into(headIView);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(LoginStatusEvent event) {
        initData();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.userInfoLayout:
                UserInfoActivity.start(context);
                break;
            case R.id.walletMenuView:
                if (Account.isLogged(context)) {
                    WalletActivity.start(context);
                }
                break;
            case R.id.guideMenuView:
                HelpActivity.start(context);
                break;
            case R.id.examLevelMenuView:
                if (Account.isLogged(context)) {
                    ExamGradeActivity.start(context, ExamGradeActivity.TYPE_UPDATE);
                }
                break;
            case R.id.feedbackMenuView:
                FeedBackActivity.start(context);
                break;
            case R.id.appScoreMenuView:
                DeviceUtil.goMarket(context);
                break;
            case R.id.aboutMenuView:
                AboutActivity.start(context);
                break;
            case R.id.logoutBtn:
                showLogoutDialog();
                break;
        }
        if (listener != null) {
            listener.onCloseMenu();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventUtil.unRegister(this);
    }

    public interface MenuListener {
        void onCloseMenu();

        void onOpenMenu();
    }
}
